#ifndef QPC_STUB_H
#define QPC_STUB_H

#include <stdlib.h>
#include <string.h>
#include <stdint.h>

struct QEvt
{
    uint16_t sig;
};
struct QActive;
typedef void QState;
#define Q_USER_SIG 100U

struct QActive
{
    void *state;
    void (*dispatch)(struct QActive *const me, struct QEvt const *const e);
    uint8_t ao_priority;
    struct QEvt **queue;
    uint8_t queue_len, queue_head, queue_tail;
};

/* 事件池静态分配 */
static struct QEvt *QF_new(uint32_t size) { return (struct QEvt *)calloc(1U, size); }
static void QF_gc(struct QEvt *e) { free(e); }

static void QACTIVE_POST(struct QActive *ao, struct QEvt *e, void *dummy)
{
    (void)dummy;
    uint8_t next = (uint8_t)((ao->queue_tail + 1U) % ao->queue_len);
    if (next != ao->queue_head)
    {
        ao->queue[ao->queue_tail] = e;
        ao->queue_tail = next;
    }
}
static struct QEvt *QACTIVE_GET(struct QActive *ao)
{
    if (ao->queue_head != ao->queue_tail)
    {
        struct QEvt *e = ao->queue[ao->queue_head];
        ao->queue_head = (uint8_t)((ao->queue_head + 1U) % ao->queue_len);
        return e;
    }
    return NULL;
}

#endif /* QPC_STUB_H */